import Vue from 'vue'
import Vuex from 'vuex'
import { Dialog } from 'cube-ui'

Vue.use(Vuex)
Vue.use(Dialog)

export default new Vuex.Store({
  state: {
    cartList: [],
    showCartList: true
  },
  mutations: {
    // isShowCartList (state) {
    //   // let validCartList = getValidCartList(state.cartList)
    //   // state.showCartList = state.showCartList === false && validCartList.length > 0
    //   state.showCartList = (state.showCartList === false)
    // },
    reduce (state, food) {
      state = packageCartList(state, food)
      countCartList(state.cartList, food.id, 'reduce')
      let validCartList = getValidCartList(state.cartList)
      if (validCartList.length === 0) {
        state.showCartList = false
      }
    },
    add (state, food) {
      state = packageCartList(state, food)
      countCartList(state.cartList, food.id, 'add')
    },
    clear (state) {
      Dialog.$create({
        type: 'confirm',
        title: '提示',
        content: '确认清空吗?',
        confirmBtn: {
          text: '确定',
          active: true,
          disabled: false,
          href: 'javascript:;'
        },
        cancelBtn: {
          text: '取消',
          active: false,
          disabled: false,
          href: 'javascript:;'
        },
        onConfirm: () => {
          state.cartList = []
          state.showCartList = false
        },
        onCancel: () => {}
      }).show()
    }
  },
  actions: {

  },
  getters: {
    getCartAllPrice: state => {
      let cartList = state.cartList
      let allPrice = 0
      if (cartList.length > 0) {
        cartList.forEach((item) => {
          allPrice = allPrice + (item.amount * item.price)
        })
      }
      return allPrice
    },
    getCartAllAmount: state => {
      let cartList = state.cartList
      let allAmount = 0
      if (cartList.length > 0) {
        cartList.forEach((item) => {
          allAmount = allAmount + item.amount
        })
      }
      return allAmount
    },
    getValidCartList: state => {
      return getValidCartList(state.cartList)
    }
  }
})

// 筛选数量大于0的购物车item
function getValidCartList (cartList) {
  return cartList.filter(item => {
    if (item.amount > 0) {
      return true
    }
  })
}

// 根据food,组装cartList数组
function packageCartList (state, food) {
  const foodId = food.id
  let cartList = state.cartList
  // 0) 根据foodId查询state中是否存在
  let currentCart = cartList.find((item) => {
    return item.id === foodId
  })
  // 1) foodId不存在,就创建item
  if (!currentCart) {
    const item = {}
    item['id'] = foodId
    item['name'] = food.name
    item['amount'] = 0
    item['price'] = food.price
    item['type'] = food.type
    cartList.push(item)
  }
  return state
}

// 根据type类型,计算cartList中amount值,type:add就加1    type:reduce就减1
function countCartList (cartList, foodId, type) {
  if (type === 'add') {
    cartList.forEach((item) => {
      if (item.id === foodId) {
        item.amount++
      }
    })
  } else if (type === 'reduce') {
    cartList.forEach((item) => {
      if (item.id === foodId && item.amount > 0) {
        item.amount--
      }
    })
  }
}
